var csInterface = new CSInterface();

// 个人主页
var homePageElement = document.querySelector("#homePage");
homePageElement.addEventListener("click", callHomepage);

function callHomepage() {
  window.cep.util.openURLInDefaultBrowser("https://gitee.com/brzjomo");
}

//调用函数
var runButtonMatBuild = document.querySelector("#run-button-matBuild");
runButtonMatBuild.addEventListener("click", matBuild);

// 添加焦点事件
// 上舌头
var topToneWidthElement = document.getElementsByName("topToneWidth")[0];
addFocusSelectEvent(topToneWidthElement);

var topToneHeightElement = document.getElementsByName("topToneHeight")[0];
addFocusSelectEvent(topToneHeightElement);

var topToneOffsetElement = document.getElementsByName("topToneOffset")[0];
addFocusSelectEvent(topToneOffsetElement);

// 主体
var mainWidthElement = document.getElementsByName("mainWidth")[0];
addFocusSelectEvent(mainWidthElement);

var mainHeightElement = document.getElementsByName("mainHeight")[0];
addFocusSelectEvent(mainHeightElement);

// 下舌头
var bottomToneWidthElement = document.getElementsByName("bottomToneWidth")[0];
addFocusSelectEvent(bottomToneWidthElement);

var bottomToneHeightElement = document.getElementsByName("bottomToneHeight")[0];
addFocusSelectEvent(bottomToneHeightElement);

var bottomToneOffsetElement = document.getElementsByName("bottomToneOffset")[0];
addFocusSelectEvent(bottomToneOffsetElement);

function matBuild() {
  // 获取电梯垫类型
  var matTypeElement = document.getElementById("matType");
  var matType = parseFloat(
    matTypeElement.options[matTypeElement.selectedIndex].value
  );

  // 上舌头
  var topToneWidth = parseFloat(topToneWidthElement.value);
  var topToneHeight = parseFloat(topToneHeightElement.value);
  var topToneOffset = parseFloat(topToneOffsetElement.value);

  // 主体
  var mainWidth = parseFloat(mainWidthElement.value);
  var mainHeight = parseFloat(mainHeightElement.value);

  // 下舌头
  var bottomToneWidth = parseFloat(bottomToneWidthElement.value);
  var bottomToneHeight = parseFloat(bottomToneHeightElement.value);
  var bottomToneOffset = parseFloat(bottomToneOffsetElement.value);

  // 传递参数
  csInterface.evalScript("matBuild(" 
    + matType  + ', ' 
    + topToneHeight + ', ' 
    + topToneWidth + ', ' 
    + topToneOffset + ', ' 
    + mainHeight + ', ' 
    + mainWidth + ', ' 
    + bottomToneHeight + ', ' 
    + bottomToneWidth + ', ' 
    + bottomToneOffset + ', ' 
    + ")");
}

// 函数用于添加焦点事件和选中文本
function addFocusSelectEvent(element) {
  if (element) {
      element.addEventListener('focus', function() {
          this.select();
      });
  }
}
